Event code: 5421
gapminderdplyrggplot2plotlyshinymagrittr for chaining functions(included in dplyr)gapminder for a great plotting datadplyr for data manipulationggplot2 for data visualizationplotly and shiny for interactive data visualizationFirst of all, updating R to LTS version.
gapminderdplyrdplyrfilter()select()arrange()mutate()summarise()group_by()filter() for subsetting rows## # A tibble: 12 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Taiwan Asia 1952 58.5 8550362 1207.
## 2 Taiwan Asia 1957 62.4 10164215 1508.
## 3 Taiwan Asia 1962 65.2 11918938 1823.
## 4 Taiwan Asia 1967 67.5 13648692 2644.
## 5 Taiwan Asia 1972 69.4 15226039 4063.
## 6 Taiwan Asia 1977 70.6 16785196 5597.
## 7 Taiwan Asia 1982 72.2 18501390 7426.
## 8 Taiwan Asia 1987 73.4 19757799 11055.
## 9 Taiwan Asia 1992 74.3 20686918 15216.
## 10 Taiwan Asia 1997 75.2 21628605 20207.
## 11 Taiwan Asia 2002 77.0 22454239 23235.
## 12 Taiwan Asia 2007 78.4 23174294 28718.
select() for extracting columns## # A tibble: 12 x 3
## year gdpPercap lifeExp
## <int> <dbl> <dbl>
## 1 1952 1207. 58.5
## 2 1957 1508. 62.4
## 3 1962 1823. 65.2
## 4 1967 2644. 67.5
## 5 1972 4063. 69.4
## 6 1977 5597. 70.6
## 7 1982 7426. 72.2
## 8 1987 11055. 73.4
## 9 1992 15216. 74.3
## 10 1997 20207. 75.2
## 11 2002 23235. 77.0
## 12 2007 28718. 78.4
arrange() for sorting rows based on certain variables## # A tibble: 33 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Myanmar Asia 2007 62.1 47761980 944
## 2 Afghanistan Asia 2007 43.8 31889923 975.
## 3 Nepal Asia 2007 63.8 28901790 1091.
## 4 Bangladesh Asia 2007 64.1 150448339 1391.
## 5 Korea, Dem. Rep. Asia 2007 67.3 23301725 1593.
## 6 Cambodia Asia 2007 59.7 14131858 1714.
## 7 Yemen, Rep. Asia 2007 62.7 22211743 2281.
## 8 Vietnam Asia 2007 74.2 85262356 2442.
## 9 India Asia 2007 64.7 1110396331 2452.
## 10 Pakistan Asia 2007 65.5 169270617 2606.
## # ... with 23 more rows
mutate() for creating new columns## # A tibble: 12 x 7
## country continent year lifeExp pop gdpPercap gdp_million
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 Taiwan Asia 1952 58.5 8550362 1207. 10320.
## 2 Taiwan Asia 1957 62.4 10164215 1508. 15326.
## 3 Taiwan Asia 1962 65.2 11918938 1823. 21727.
## 4 Taiwan Asia 1967 67.5 13648692 2644. 36085.
## 5 Taiwan Asia 1972 69.4 15226039 4063. 61856.
## 6 Taiwan Asia 1977 70.6 16785196 5597. 93939.
## 7 Taiwan Asia 1982 72.2 18501390 7426. 137398.
## 8 Taiwan Asia 1987 73.4 19757799 11055. 218414.
## 9 Taiwan Asia 1992 74.3 20686918 15216. 314765.
## 10 Taiwan Asia 1997 75.2 21628605 20207. 437045.
## 11 Taiwan Asia 2002 77.0 22454239 23235. 521734.
## 12 Taiwan Asia 2007 78.4 23174294 28718. 665526.
summarise() for a summary## # A tibble: 1 x 1
## `median(gdpPercap)`
## <dbl>
## 1 3532.
group_by() for a grouped summary## # A tibble: 5 x 2
## continent medianGdpPercap
## <fct> <dbl>
## 1 Africa 1192.
## 2 Americas 5466.
## 3 Asia 2647.
## 4 Europe 12082.
## 5 Oceania 17983.
dplyrggplot2Grammar of graphics.
ggplot(aes(x = , y = , color = , fill = , ...)) for data mappinggeom_OOO() for different charts`+ to add different layersgeom_point() for exploring correlationsgeom_histogram() for exploring distributionsgeom_bar() for exploring row countsgeom_bar() for grouped summaryggplot2plotlyplotlyCreate interactive, D3 and WebGL charts in R.
plotlyConverting ggplot2 graphs to interactive versions with ggplotly().
plotlyplotlyshinyshinyShiny is an R package that makes it easy to build interactive web applications (apps) straight from R.
app.Rapp.R lives in a directory (for example, newdir/) and the app can be run with runApp(“newdir”)shinyApp() function creates Shiny app objects# app.R
library(gapminder)
library(shiny)
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "histPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$histPlot <- renderPlot({
x <- gapminder$gdpPercap
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "GDP Per Capita",
main = "Histogram of GDP Per Capita")
})
}
shinyApp(ui = ui, server = server)shiny and plotly# app.R
# Library packages
# install.packages(c("gapminder", "plotly", "shiny"))
library(gapminder)
library(plotly)
library(shiny)
library(dplyr)
# Globar variables
bubble_radius <- sqrt(gapminder$pop / pi)
unique_continents <- unique(gapminder$continent)
range_gdpPercap <- log10(range(gapminder$gdpPercap) + c(-200, 20000))
range_lifeExp <- range(gapminder$lifeExp) + c(-30, 30)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("R Gapminder Replica"),
# Sidebar panel
sidebarLayout(
# CheckboxGroup input
sidebarPanel(
checkboxGroupInput(
"continents",
"Continents:",
choices = unique_continents,
selected = unique_continents
)
),
# Plotly rendering
mainPanel(
plotlyOutput("gapminder_bubble")
)
)
)
# Define server logic
server <- function(input, output) {
# reactive filtering
reactive_gapminder <- reactive(
gapminder %>%
filter(continent %in% input$continents)
)
output$gapminder_bubble <- renderPlotly({
validate(
need(input$continents, 'Check at least one continent!')
)
reactive_gapminder() %>%
plot_ly(x = ~gdpPercap, y = ~lifeExp,
size = ~pop, type = "scatter", mode = "markers",
color = ~continent, text = ~country, frame = ~year, hoverinfo = "text",
sizes = c(min(bubble_radius), max(bubble_radius))) %>%
layout(xaxis = list(type = "log",
range = range_gdpPercap),
yaxis = list(range = range_lifeExp))
})
}
# Run the application
shinyApp(ui = ui, server = server)mayor <- readRDS(url("https://s3-ap-northeast-1.amazonaws.com/tw-election-2018/mayor.rds"))
dim(mayor)
head(mayor)
tail(mayor)
summary(mayor)
str(mayor)## [1] 62689 7
## admin_area district village office votes party candidate
## 1 台北市 北投區 建民里 1 4 無黨籍 吳蕚洋
## 2 台北市 北投區 建民里 2 2 無黨籍 吳蕚洋
## 3 台北市 北投區 建民里 3 2 無黨籍 吳蕚洋
## 4 台北市 北投區 文林里 4 1 無黨籍 吳蕚洋
## 5 台北市 北投區 文林里 5 5 無黨籍 吳蕚洋
## 6 台北市 北投區 文林里 6 3 無黨籍 吳蕚洋
## admin_area district village office votes
## 62684 連江縣 南竿鄉 馬祖村、津沙村、四維村、仁愛村 4 654
## 62685 連江縣 北竿鄉 后沃村、橋仔村、塘岐村 5 838
## 62686 連江縣 北竿鄉 坂里村、白沙村、芹壁村 6 301
## 62687 連江縣 莒光鄉 田沃村、西坵村、青帆村 7 341
## 62688 連江縣 莒光鄉 大坪村、福正村 8 391
## 62689 連江縣 東引鄉 樂華村、中柳村 9 396
## party candidate
## 62684 中國國民黨 劉增應
## 62685 中國國民黨 劉增應
## 62686 中國國民黨 劉增應
## 62687 中國國民黨 劉增應
## 62688 中國國民黨 劉增應
## 62689 中國國民黨 劉增應
## admin_area district village office
## Length:62689 Length:62689 Length:62689 Min. : 1.0
## Class :character Class :character Class :character 1st Qu.: 190.0
## Mode :character Mode :character Mode :character Median : 457.0
## Mean : 602.8
## 3rd Qu.: 934.0
## Max. :2446.0
## votes party candidate
## Min. : 0.0 Length:62689 Length:62689
## 1st Qu.: 12.0 Class :character Class :character
## Median : 148.0 Mode :character Mode :character
## Mean : 199.5
## 3rd Qu.: 358.0
## Max. :1237.0
## 'data.frame': 62689 obs. of 7 variables:
## $ admin_area: chr "台北市" "台北市" "台北市" "台北市" ...
## $ district : chr "北投區" "北投區" "北投區" "北投區" ...
## $ village : chr "建民里" "建民里" "建民里" "文林里" ...
## $ office : int 1 2 3 4 5 6 7 8 9 10 ...
## $ votes : int 4 2 2 1 5 3 0 4 5 3 ...
## $ party : chr "無黨籍" "無黨籍" "無黨籍" "無黨籍" ...
## $ candidate : chr "吳蕚洋" "吳蕚洋" "吳蕚洋" "吳蕚洋" ...